package com.css.dzj.dzjap.modules.force.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.administration.check.entity.ApJgxwCheckAction;
import com.css.dzj.dzjap.modules.force.entity.ApJgxxForceEntity;
import com.css.dzj.dzjap.modules.force.service.ForceService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/force")
public class ForceController {

    @Autowired
    private ForceService forceService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 添加(行政强制行为）监管信息信用表
     */
    @PostMapping(value = "/addForce")
    public R addForce(@RequestBody ApJgxxForceEntity apJgxxForceEntity){
        String s;
        try{
            String msg = forceService.addForce(apJgxxForceEntity);
            s = JSON.toJSONString(apJgxxForceEntity);
            sysAuditService.saveSuccessAudit("行政强制行为", Constant.FORCE,Constant.PT,Constant.ADD,s,request);
            return R.ok().put("data", msg);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(apJgxxForceEntity);
            sysAuditService.saveFailAudit("行政强制行为",Constant.FORCE,Constant.PT,Constant.ADD,s,e,request);
            return R.error("操作失败");
        }

    }
    /**
     * 通过ID删除(行政强制行为）监管信息信用表
     */
    @PostMapping(value = "/delForce")
    public R delForce(@RequestParam("forceId") String forceId){
        String s;
        if(forceService.delForce(forceId)){
            s = JSON.toJSONString(forceId);
            sysAuditService.saveSuccessAudit("行政强制行为", Constant.FORCE,Constant.PT,Constant.DEL,s,request);
            return R.ok("操作成功!");
        }else{
            RuntimeException e = new RuntimeException("操作失败");
            s = JSON.toJSONString(forceId);
            sysAuditService.saveFailAudit("行政强制行为",Constant.FORCE,Constant.PT,Constant.DEL,s,e,request);
            return  R.error("操作失败!");
        }
    }
    /**
     * 修改（行政强制行为）监管信息信用表
     */
    @PostMapping(value = "/upForce")
    public R updateForce(@RequestBody ApJgxxForceEntity apJgxxForceEntity){
        String s;
        try {
            ApJgxxForceEntity force = forceService.getForce(apJgxxForceEntity.getForceId());
            BeanUtil.copyBeanNotNullToBean(apJgxxForceEntity, force);
            forceService.updateForce(force);
            s = JSON.toJSONString(apJgxxForceEntity);
            sysAuditService.saveSuccessAudit("行政强制行为", Constant.FORCE,Constant.PT,Constant.UPDATE,s,request);
            return R.ok();
        }catch (Exception e){
            s = JSON.toJSONString(apJgxxForceEntity);
            sysAuditService.saveFailAudit("行政强制行为",Constant.FORCE,Constant.PT,Constant.UPDATE,s,e,request);
            return  R.error("操作失败!");
        }

    }
    /**
     * 通过ID查询（行政强制行为）监管信息信用表
     */
    @GetMapping(value = "/getForce")
    public R getForce(@RequestParam("forceId") String forceId){
        String s;
        try {
            ApJgxxForceEntity force = forceService.getForce(forceId);
            s = JSON.toJSONString(force);
            sysAuditService.saveSuccessAudit("行政强制行为", Constant.FORCE,Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data", force);
        }catch (Exception e){
            s = JSON.toJSONString(forceId);
            sysAuditService.saveFailAudit("行政强制行为",Constant.FORCE,Constant.PT,Constant.SELETE,s,e,request);
            return  R.error("操作失败!");
        }

    }
    /**
     *分页查询信息表
     */
    @GetMapping(value = "/getAllForce")
    public R getAllForce(@RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize) {
        String s="";
       try {
           PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
           QueryResults results = forceService.getAllForce(pageRequest);
           s = JSON.toJSONString(results);
           sysAuditService.saveSuccessAudit("行政强制行为", Constant.FORCE,Constant.PT,Constant.PAGE,s,request);
           return R.ok().put("data", results);
       }catch (Exception e){
           sysAuditService.saveFailAudit("行政强制行为",Constant.FORCE,Constant.PT,Constant.PAGE,s,e,request);
           return  R.error("操作失败!");
       }

    }
    /**
     * 条件查询
     */
    @GetMapping(value ="/getAllContation")
    public R getAllConOther(@RequestParam String superviseId,
                            @RequestParam(required = false) String forceActionName,
                            @RequestParam(required = false) String forceActionCode,
                            @RequestParam(required = false) String submittedDate,
                            @RequestParam(required = false) Integer pageNo,
                            @RequestParam(required = false) Integer pageSize){
        String s="";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
            QueryResults<ApJgxxForceEntity> allContation = forceService.getAllContation(pageRequest, superviseId, forceActionName, forceActionCode, submittedDate);
            s = JSON.toJSONString(allContation);
            sysAuditService.saveSuccessAudit("行政强制行为", Constant.FORCE,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", allContation);
        }catch (Exception e){
            sysAuditService.saveFailAudit("行政强制行为",Constant.FORCE,Constant.PT,Constant.PAGE,s,e,request);
            return  R.error("操作失败!");
        }

    }

    /***
     * 获取强制行为
     * @return
     */
    @GetMapping("/force")
    public R countForce(){
        HashMap count=forceService.getCount();
        return R.ok().put("count",count);
    }
    /***
     * 获取强制行为
     * @return
     */
    @GetMapping("/forceyear")
    public R countForceByYear(String year){
        HashMap count=forceService.getCountByYear(year);
        return R.ok().put("count",count);
    }
    /***
     * 获取当前登陆人的机构的强制行为
     * @param request
     * @return
     */
    @GetMapping("/getforce")
    public R getOpForce(HttpServletRequest request){
        Long count=forceService.getOpCheck(request);
        return R.ok().put("count",count);
    }

   /**
   *@Author weitao
   *@Date 14:58 2020/4/29
    * 多条件查询强制行为
   */
   @GetMapping(value ="/getWtContation")
   public R getWtContation(@RequestParam String superviseId,
                           @RequestParam(required = false) String forceActionName,
                           @RequestParam(required = false) String implementInstitution,
                           @RequestParam(required = false) Integer pageNo,
                           @RequestParam(required = false) Integer pageSize){
       String s="";
       try {
           PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
           QueryResults<ApJgxxForceEntity> foreResult = forceService.getWtContation(superviseId,forceActionName,implementInstitution,pageRequest);
           return R.ok().put("data", foreResult);
       }catch (Exception e){
           return  R.error("操作失败!");
       }

   }

    /**
     * 根据参数导出数据到Excel
     */
    @GetMapping(value ="/getWtForceList")
    public List<ApJgxxForceEntity> getWtContation(@RequestParam String superviseId,
                            @RequestParam(required = false) String forceActionName,
                            @RequestParam(required = false) String implementInstitution){

       List<ApJgxxForceEntity> forceList = forceService.forceList(superviseId,forceActionName,implementInstitution);

        return forceList;
    }

    /**
     *根据ids查询导出数据
     */
    @GetMapping(value ="/getWtForceIds")
    public List<ApJgxxForceEntity> queryByIds(@RequestParam String ids){

        List<ApJgxxForceEntity> forceList = forceService.queryByIds(ids);
        return forceList;
    }

   /**
   *@Author weitao
   *@Date 15:29 2020/4/29
    * 多条件导出
   */
   @GetMapping("/export")
   public R export(HttpServletResponse response,
                   @RequestParam String superviseId,
                   @RequestParam(required = false) String forceActionName,
                   @RequestParam(required = false) String implementInstitution) {
       String s="";
       try {
           List<ApJgxxForceEntity> foreResult = forceService.forceList(superviseId,forceActionName,
                   implementInstitution);
           forceService.exportForceToExcel(response, foreResult);
           return R.ok();
       } catch (Exception e) {
           e.printStackTrace();
           return R.error(e.getMessage());
       }
   }

    /**
     *导出选中行数据
     */
    @GetMapping("/exportByIds")
    public R exportByIds(HttpServletResponse response, @RequestParam String ids) {
        String s="";
        try {
            List<ApJgxxForceEntity> forceList = forceService.queryByIds(ids);
            forceService.exportForceToExcel(response, forceList);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     *@Author weitao
     *@Date 14:39 2020/4/29
     * 统计数量
     */
    @GetMapping("/forceCount")
    public R forceCount(){

        try {
            String forceCount = forceService.getForceCount();
            return R.ok().put("data",forceCount);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过ID查询（行政强制行为）监管信息信用表
     */
    @GetMapping(value = "/getForceID")
    public R getForceID(@RequestParam("forceId") String forceId){
        String s;
        try {
            ApJgxxForceEntity force = forceService.getForce(forceId);
            return R.ok().put("data", force);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }

    }
    /**
     * 查询行政强制行为所有信息
     * 对外提供接口
     *@Author weitao
     *@Date 14:22 2020/6/28
     */
    @GetMapping(value ="/getApList")
    public List<ApJgxxForceEntity> getApList(){

        try {
            List<ApJgxxForceEntity> list = forceService.getApList();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
